package _06_动态规划;

public class _64_最小路径和 {


    public int minPathSum(int[][] grid) {
        int rows = grid.length;
        int cols = grid[0].length;

        // 定义dp数组, 第row行第col列的最小路劲
        int[][] dp = new int[rows][cols];
        // 初始化dp数组
        dp[0][0] = grid[0][0];
        for (int col = 1; col < cols; col++) {
            dp[0][col] = dp[0][col - 1] + grid[0][col];
        }
        for (int row = 1; row < rows; row++) {
            dp[row][0] = dp[row - 1][0] + grid[row][0];
        }
        for (int row = 1; row < rows; row++) {
            for (int col = 1; col < cols; col++) {
                dp[row][col] = Math.min(dp[row - 1][col], dp[row][col - 1]) + grid[row][col];
            }
        }
        return dp[rows - 1][cols - 1];
    }

}
